<template>
    <el-dialog v-model="dialogFormVisible" title='删除申请' width="30%" center >
        <span >是否确定删除该申请</span>
        <template #footer>
            <span class="dialog-footer">
                <el-button type="primary" @click="deleteApply">
                    确定
                </el-button>
                <el-button @click="dialogFormVisible = false">
                    取消
                </el-button>
            </span>
        </template>
    </el-dialog>
</template>

<script lang="ts" setup>
import { onBeforeUnmount, ref } from 'vue'
import {
    bus
} from "@/utils/mitt.js"
import { ElMessage } from 'element-plus'
import {deleteBursaryApplication, deleteScholarshipApplication} from '@/api/apply.js'


const bursaryId = ref()
const scholarshipId = ref()

bus.on('deleteB', (id: number) => {
    bursaryId.value = id
})
bus.on('deleteS', (id: number) => {
    scholarshipId.value = id
})
// 删除管理员
const emit = defineEmits(['success'])
const deleteApply = async () => {
    if (bursaryId.value) {
        const res = await deleteBursaryApplication(bursaryId.value)
        if (res.data.status === 0) {
        	ElMessage.success(res.data.message)
        	dialogFormVisible.value = false
            emit('success')
        } else {
        	ElMessage.error(res.data.message)
        	dialogFormVisible.value = false
        }
    }
    if (scholarshipId.value) {
        const res = await deleteScholarshipApplication(scholarshipId.value)
        if (res.data.status === 0) {
            ElMessage.success(res.data.message)
            dialogFormVisible.value = false
            emit('success')
        } else {
            ElMessage.error(res.data.message)
            dialogFormVisible.value = false
        }
    }

}

// 弹窗开关
const dialogFormVisible = ref(false)

// 打开弹窗
const open = () => {
    dialogFormVisible.value = true
}

defineExpose({
    open
})

onBeforeUnmount(() => {
    bus.all.clear()
})
</script>

<style lang="scss" scoped></style>